/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: javase - T23
 * <p>Powered by webrx On 2021-12-27 16:43:39
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T23 {
    AtomicInteger n = new AtomicInteger(50);
    int num = 50;

    void minus() {
        while (true) {
            synchronized (this) {
                if (n.get() > 0) {
                    n.decrementAndGet();
                    System.out.printf("%s 售出一张票，剩余%d张票。 %n", Thread.currentThread().getName(), n.get());
                } else {
                    break;
                }

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        var t = new T23();
        new Thread(t::minus, "T1").start();
        new Thread(t::minus, "T2").start();
        new Thread(t::minus, "T3").start();
        new Thread(t::minus, "T4").start();
        new Thread(t::minus, "T5").start();
        new Thread(t::minus, "T6").start();
        new Thread(t::minus, "T7").start();
        new Thread(t::minus, "T8").start();
    }
}
